#include <QApplication>
#include <QEventLoop>

#include "UIApplicationDelegate.h"
#include "UIApplication.h"

// ===========================================================================
//  PUBLIC UIApplicationMain Function
// ===========================================================================
int UIApplicationMain (int argc, char **argv, 
                       UIApplicationDelegate *appDelegate)
{
    if (appDelegate == NULL)
        return(1);

    QEventLoop appLoop;


    UIApplication *application = UIApplication::sharedApplication();

    // Quit Prev App.
    if (application->keyWindow() != NULL)
        application->quit();

    // Setup new Application
    QObject::connect(application, SIGNAL(terminated()), &appLoop, SLOT(quit()));
    application->setArguments(argc, argv);
    application->setDelegate(appDelegate);

    // Call Application Did Finish Launching Delegate
    appDelegate->applicationDidFinishLaunching(application);

    return(appLoop.exec());
}

// ===========================================================================
//  PUBLIC Constructor/Destructor
// ===========================================================================
UIApplicationDelegate::UIApplicationDelegate() {
}

UIApplicationDelegate::~UIApplicationDelegate() {
}

// ===========================================================================
//  PUBLIC Virtual Methods
// ===========================================================================
/**
 * Tells the delegate when the application is ready to start.
 * Put your Init/Startup Code Here.
 */
void UIApplicationDelegate::applicationDidFinishLaunching (
    UIApplication *application)
{
    Q_UNUSED(application)
}

/**
 * Tells the delegate when the application is about to terminate. 
 */
void UIApplicationDelegate::applicationWillTerminate (
    UIApplication *application)
{
    Q_UNUSED(application)
}

/**
 * Tells the delegate when the application receives a memory warning. 
 */
void UIApplicationDelegate::applicationDidReceiveMemoryWarning (
    UIApplication *application)
{
    Q_UNUSED(application)
}

/**
 * Asks the delegate to open a resource identified by Url.
 */        
bool UIApplicationDelegate::applicationHandleOpenUrl (
    UIApplication *application, 
    const QUrl& url)
{
    Q_UNUSED(application)
    Q_UNUSED(url)
    return(false);
}

